<template>
  <el-dialog title="试卷单词列表" :visible.sync="localOpen" width="800px" :close-on-click-modal="true" @close="cancel"
    append-to-body>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单词" prop="wordContent">
        <el-input v-model="queryParams.wordContent" placeholder="请输入单词" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['datacenter:filebody:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="filebodyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="出现次数" align="center" prop="wordCount" />
      <el-table-column label="单词内容" align="center" prop="wordContent" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['datacenter:filebody:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </el-dialog>
</template>

<script>
  import {
    listFilebody,
    delFilebody
  } from "@/api/bus_web/datacenter/filebody";

  export default {
    name: "FileListbody",
    props: {
      open: Boolean,
      fileId:Number
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        localOpen:this.open,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // (教育-统计)文件内容表格数据
        filebodyList: [],
        // 弹出层标题
        title: "",
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          fileId: null,
          wordCount: null,
          wordContent: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },

    watch: {
      open(newValue, oldValue) {
        this.localOpen = newValue; // 同步父组件的 open 值到本地状态
      },
      localOpen(newValue) {
        this.reset()
        if (newValue) {
          this.rows = []
          this.getList() // 当本地状态变为 false 时，通知父组件
        } else {
          this.cancel()
        }
      }
    },
    methods: {
      /** 查询(教育-统计)文件内容列表 */
      getList() {
        this.loading = true;
        this.queryParams.fileId=this.fileId
        listFilebody(this.queryParams).then(response => {
          this.filebodyList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          fileId: null,
          wordCount: null,
          wordContent: null,
          deleted: null,
          createBy: null,
          updateBy: null,
          gmtCreate: null,
          gmtModify: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除试卷单词内容编号为"' + ids + '"的数据项？').then(function() {
          return delFilebody(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      // 取消按钮
      cancel() {
        this.$emit('close-select-window')
      }
    }
  };
</script>
